package ssov5

import (
	"fmt"
	"time"

	"gitee.com/micro-plat/sso/apiserver/api/modules/biz/member"
	"github.com/micro-plat/lib4go/net"
	"github.com/micro-plat/lib4go/security/md5"
	"github.com/micro-plat/lib4go/types"
)

func remotingLogin(userName, password string) (interface{}, error) {
	values := net.NewValues()
	values.Set("user_name", userName)
	values.Set("password", password)
	values.Set("ident", Config.ident)
	values.Set("timestamp", types.GetString(time.Now().Unix()))

	values = values.Sort()
	raw := values.Join("", "") + Config.secret
	values.Set("sign", md5.Encrypt(raw))

	lgState := &member.MemberState{}
	_, _, err := remoteRequest(Config.host, passwordLogin, values.Encode(), lgState)
	if err != nil {
		return nil, err
	}
	return lgState, nil
}
func remotingGetMenu(userID int64) (interface{}, error) {
	values := net.NewValues()
	values.Set("user_id", fmt.Sprint(userID))
	values.Set("ident", Config.ident)
	values.Set("timestamp", types.GetString(time.Now().Unix()))

	values = values.Sort()
	raw := values.Join("", "") + Config.secret
	values.Set("sign", md5.Encrypt(raw))

	buff, _, err := remoteRequestString(Config.host, userMenu, values.Encode())
	if err != nil {
		return nil, err
	}
	return string(buff), nil
}
func remotingSystem() (interface{}, error) {
	values := net.NewValues()
	values.Set("ident", Config.ident)
	values.Set("timestamp", types.GetString(time.Now().Unix()))

	values = values.Sort()
	raw := values.Join("", "") + Config.secret
	values.Set("sign", md5.Encrypt(raw))

	buff, _, err := remoteRequestString(Config.host, systemByIdent, values.Encode())
	if err != nil {
		return nil, err
	}
	return string(buff), nil
}
